/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.platform.dto.EquiomentAlarmLogDTO;
import org.springblade.modules.platform.entity.EquiomentAlarmLog;
import org.springblade.modules.platform.entity.EquipmentAlarm;
import org.springblade.modules.platform.service.IEquiomentAlarmLogService;
import org.springblade.modules.platform.service.IEquipmentAlarmService;
import org.springblade.modules.platform.vo.EquiomentAlarmLogListVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 *  控制器
 *
 * @author BladeX
 * @since 2020-11-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/equiomentalarmlog")
@Api(value = "", tags = "设备管理-设备告警")
public class EquiomentAlarmLogController extends BladeController {

	private final IEquiomentAlarmLogService equiomentAlarmLogService;
	private final IEquipmentAlarmService iEquipmentAlarmService;


	@ApiLog("设备管理-设备告警-记录")
	@GetMapping("/getList")
	@ApiOperation(value = "设备告警记录", notes = "传入equiomentAlarmLogDTO")
	public R<EquiomentAlarmLogListVO> gatewayListVOR(EquiomentAlarmLogDTO equiomentAlarmLogDTO, Query query){
		EquiomentAlarmLogListVO equiomentAlarmLogListVO = equiomentAlarmLogService.selectPage1(equiomentAlarmLogDTO,query);
		return R.data(equiomentAlarmLogListVO);
	}
	@ApiLog("设备管理-设备告警-详情")
	@GetMapping("/Details")
	@ApiOperation(value = "设备告警Details", notes = "id")
	public R<EquiomentAlarmLog> gatewayDetails(Integer id){
		EquiomentAlarmLog byId = equiomentAlarmLogService.getById(id);
		return R.data(byId);
	}
	/**
	 * 修改
	 */
	@ApiLog("设备管理-设备告警-处理")
	@PostMapping("/update")
	@ApiOperation(value = "处理", notes = "传入equiomentAlarmLog")
	public R update(@Valid @RequestBody List<EquiomentAlarmLog> equiomentAlarmLogs) {
		equiomentAlarmLogs.forEach(equiomentAlarmLog->{
			EquiomentAlarmLog byId = equiomentAlarmLogService.getById(equiomentAlarmLog.getId());
			byId.setUpdateTime(System.currentTimeMillis());
			byId.setProcessingStatus(1);
			byId.setRemark(equiomentAlarmLog.getRemark());
			EquipmentAlarm one = iEquipmentAlarmService.getOne(Wrappers.<EquipmentAlarm>query().lambda()
				.eq(EquipmentAlarm::getIsAlert, true)
				.eq(EquipmentAlarm::getId, equiomentAlarmLog.getAlarmId()));
			if(one!=null){
				one.setIsAlert(false);
				iEquipmentAlarmService.updateById(one);
			}
			equiomentAlarmLogService.updateById(byId);
		});
		return R.status(true);
	}


	@ApiLog("设备管理-设备告警-批量删除")
	@PostMapping("/delete")
	@ApiOperation(value = "批量删除", notes = "传入equiomentAlarmLog")
	public R delete(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(equiomentAlarmLogService.removeByIds(Func.toLongList(ids)));
	}
}
